package com.org.myapplication.Manager;

import android.content.Context;
import android.os.ServiceManager;
import android.util.Log;

import com.org.sc.realtimekeystone.IVL53L5AidlInterface;

public class VL53L5CXServiceManager {
    private static VL53L5CXServiceManager instance;
    public IVL53L5AidlInterface myservice;
    private Context mcontext;
    private final static String TAG = "VL53L5CXServiceManager";

    public static VL53L5CXServiceManager getInstance(Context context) {
        if (instance == null) {
            instance = new VL53L5CXServiceManager(context);
        }
        return instance;
    }

    public VL53L5CXServiceManager(Context context) {
        this.mcontext = context;
        while (myservice == null) {
            myservice = IVL53L5AidlInterface.Stub.asInterface(ServiceManager.getService("SCRealTimeKeyStoneService"));
            Log.e(TAG, "############[MagicServiceManager][getMyservice][服务是空的]");
        }
        Log.i(TAG, "############[MagicServiceManager][getMyservice][服务已经拿到]");
    }


    /***
     *设置打开和关闭对焦功能
     *@param  status:  true:开 false:关
     **/
    public void set_af_state(boolean status) {
        try {
            Log.i(TAG, "============发送命令：[set_af_state] parameter = " + status);
            myservice.fc_set_af_state(status);
        } catch (Exception e) {
            Log.e(TAG, "[set_af_state]:" + e.getMessage());
        }
    }

    //获取打开和关闭对焦功能
    public boolean get_af_state() {
        boolean status = false;
        try {
            status = myservice.fc_get_af_state();
        } catch (Exception e) {
            Log.e(TAG, "[get_af_state]:" + e.getMessage());
        }
        Log.i(TAG, "============发送命令：[get_af_state] status = " + status);
        return status;
    }

    //触发一次自动对焦
    public void do_once_af() {
        try {
            Log.i(TAG, "============发送命令：[do_once_af] ");
            myservice.fc_do_once_af();
        } catch (Exception e) {
            Log.e(TAG, "[do_once_af]:" + e.getMessage());
        }
    }

    //开启电机
    public void open_motor() {
        try {
            Log.i(TAG, "============发送命令：[open_motor] ");
            myservice.fc_open_motor();
        } catch (Exception e) {
            Log.e(TAG, "[open_motor]:" + e.getMessage());
        }
    }

    //手动FOCUS+ (def step=1)
    public void stepFW() {
        try {
            Log.i(TAG, "============发送命令：[stepFW] ");
            myservice.fc_stepFW();
        } catch (Exception e) {
            Log.e(TAG, "[stepFW]:" + e.getMessage());
        }
    }

    //手动FOCUS- (def step=1)
    public void stepBK() {
        try {
            Log.i(TAG, "============发送命令：[stepBK] ");
            myservice.fc_stepBK();
        } catch (Exception e) {
            Log.e(TAG, "[stepBK]:" + e.getMessage());
        }
    }

    //关闭电机
    public void close_motor() {
        try {
            Log.i(TAG, "============发送命令：[close_motor] ");
            myservice.fc_close_motor();
        } catch (Exception e) {
            Log.e(TAG, "[close_motor]:" + e.getMessage());
        }
    }

    //设置打开和关闭自动校正功能
    public void set_corr_state(boolean status) {
        try {
            Log.i(TAG, "============发送命令：[set_corr_state] parameter = " + status);
            myservice.fc_set_corr_state(status);
        } catch (Exception e) {
            Log.e(TAG, "[set_corr_state]:" + e.getMessage());
        }
    }

    //获取打开和关闭自动校正功能
    public boolean get_corr_state() {
        boolean status = false;
        try {
            status = myservice.fc_get_corr_state();
        } catch (Exception e) {
            Log.e(TAG, "[get_corr_state]:" + e.getMessage());
        }
        Log.i(TAG, "============发送命令：[get_corr_state] status = " + status);
        return status;
    }

    //触发一次自动校正
    public void do_once_corr() {
        try {
            Log.i(TAG, "============发送命令：[do_once_corr] ");
            myservice.fc_do_once_corr();
        } catch (Exception e) {
            Log.e(TAG, "[do_once_corr]:" + e.getMessage());
        }
    }

    public void fun_srceen_zoom(int type, double mpx, double hpx) {
        try {
            Log.i(TAG, "============发送命令：[fun_srceen_zoom] ");
            myservice.fun_srceen_zoom(type, mpx, hpx);
        } catch (Exception e) {
            Log.e(TAG, "[fun_srceen_zoom]:" + e.getMessage());
        }
    }
}
